//
//  ListViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/8.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "ListViewController.h"

@interface ListViewController ()

@end

@implementation ListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initDataSource];
    
    [self initTableView];
}

- (void)initDataSource{
    NSMutableArray * models = [NSMutableArray array];
    
    NSArray * data = @[@{@"imageName":@"直播列表_背景@2x.png",
                         @"name":@"仁泽法师",
                         @"introduction":@"111菠萝蜜多心经仁泽法师讲解菠萝蜜蜜多心经 仁者无敌",
                         @"person_Number":@"12345"},
                       @{@"imageName":@"直播列表_背景@2x.png",
                         @"name":@"甜心法师",
                         @"introduction":@"222111菠萝蜜多心经仁泽法师讲解菠萝蜜蜜多心经 仁者无敌",
                         @"person_Number":@"54321"}];
    
    for (int i = 0; i < 2; i ++) {
        ListModel * list = [[ListModel alloc] initWithContentDic:data[i]];
        [models addObject:list];
    }
    _dataList = models;
}

- (void)initTableView{
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, kScreen_height - 64 - 48) style:UITableViewStyleGrouped];
    tableView.dataSource = self;
    tableView.delegate = self;
    [self.view addSubview:tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataList.count + 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        // 标题
        static NSString * reuseIdentifier = @"titleCellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            // 创建cell
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            cell.textLabel.font = [UIFont systemFontOfSize:10 weight:2];
            cell.textLabel.textColor = kColor_title_color;
            VOSegmentedControl *segctrl = [[VOSegmentedControl alloc] initWithSegments:@[@{@"text": @"周榜"},@{@"text": @"月榜"},@{@"text": @"总榜"}]];
            segctrl.contentStyle = VOContentStyleTextAlone;
            segctrl.indicatorStyle = VOSegCtrlIndicatorStyleBottomLine;
            segctrl.allowNoSelection = NO;
            segctrl.textColor = kColor_text_color;
            segctrl.selectedTextColor = kColor_text_color;
            segctrl.textFont = [UIFont systemFontOfSize:12];
            segctrl.frame = CGRectMake(kScreen_width - 140 - 10, 0, 140, 38);
            segctrl.selectedTextFont = [UIFont systemFontOfSize:14 weight:2];
            segctrl.indicatorThickness = 0;
            segctrl.tag = indexPath.section;
            [cell.contentView addSubview:segctrl];
            [segctrl addTarget:self action:@selector(segmentCtrlValuechange:) forControlEvents:UIControlEventValueChanged];
        }
        if (indexPath.section == 0) {
            cell.textLabel.text = @"更新榜";
        } else{
            cell.textLabel.text = @"热门榜";
        }
        return cell;
    } else if (indexPath.row != 0 && indexPath.row < _dataList.count + 1){
        ListTableViewCell * cell = [ListTableViewCell cellWithTableView:tableView];
        cell.list = _dataList[indexPath.row - 1];
        return cell;
    } else{
        // 查看更多内容
        static NSString * reuseIdentifier = @"moreCellIdentifier";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
        if (cell == nil) {
            // 创建cell
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
            
            UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake((kScreen_width - 110) / 2, 0, 110, 36)];
            [cell.contentView addSubview:bgView];
            
            // 提示文本
            UILabel * moreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 85, 36)];
            moreLabel.textColor = kColor_moreContent_color;
            moreLabel.textAlignment = NSTextAlignmentCenter;
            moreLabel.text = @"更多热门内容";
            moreLabel.font = [UIFont systemFontOfSize:13];
            [bgView addSubview:moreLabel];
            
            // 图标
            UIImageView * moreIcon = [[UIImageView alloc] initWithFrame:CGRectMake(moreLabel.right, 5.5, 25, 25)];
            moreIcon.image = [UIImage imageNamed:@"前进50px.png"];
            [bgView addSubview:moreIcon];
        }
        return cell;
    }
}


- (void)segmentCtrlValuechange: (VOSegmentedControl *)segmentCtrl{
    NSLog(@"%@: value --> %@",@(segmentCtrl.tag), @(segmentCtrl.selectedSegmentIndex));
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)sectio{
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        // 标题
        return 38;
    } else if (indexPath.row == _dataList.count + 1){
        // 查看更多内容
        return 36;
    }
    else{
        return 4 * 2 + 67;
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
